﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace A_Modules.CheckBox_Header_Column_For_DataGridView
{
    public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
    {
        bool _bChecked;
        public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked)
        {
            _bChecked = bChecked;
        }
        public bool Checked
        {
            get { return _bChecked; }
        }
    }
    public delegate void CheckBoxClickedHandler(bool state);
    class DataGridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell
    {
        public event CheckBoxClickedHandler OnCheckBoxClicked;

        private Point _checkBoxLocation;
        private Point _cellLocation = new Point();
        private Size _checkBoxSize;

        bool _checked = false;

        //VisualStyles
        System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
            System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;




        protected override void Paint(
            System.Drawing.Graphics graphics,
            System.Drawing.Rectangle clipBounds,
            System.Drawing.Rectangle cellBounds,
            int rowIndex,
            DataGridViewElementStates dataGridViewElementState,
            object value,
            object formattedValue,
            string errorText,
            DataGridViewCellStyle cellStyle,
            DataGridViewAdvancedBorderStyle advancedBorderStyle,
            DataGridViewPaintParts paintParts)
        {
            //1.Base Paint
            base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                dataGridViewElementState, value,
                formattedValue, errorText, cellStyle,
                advancedBorderStyle, paintParts);

            //2.
            Point checkboxPoint = new Point();//
            
            Size checkBoxSize = CheckBoxRenderer.GetGlyphSize(
                                        graphics,
                                        System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);

            checkboxPoint.X = cellBounds.Location.X +
                (cellBounds.Width / 2) - (checkBoxSize.Width / 2);

            checkboxPoint.Y = cellBounds.Location.Y +
                (cellBounds.Height / 2) - (checkBoxSize.Height / 2);

            //cache position
            _cellLocation = cellBounds.Location;
            _checkBoxLocation = checkboxPoint;
            _checkBoxSize = checkBoxSize;

            //Checked Style
            if (_checked)
                _cbState = System.Windows.Forms.VisualStyles.
                    CheckBoxState.CheckedNormal;
            else
                _cbState = System.Windows.Forms.VisualStyles.
                    CheckBoxState.UncheckedNormal;

            //Drawing
            CheckBoxRenderer.DrawCheckBox(graphics, _checkBoxLocation, _cbState);
        }


        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
            Point p = 
                new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);

            //Click Area
            if (p.X >= _checkBoxLocation.X && p.X <=
                _checkBoxLocation.X + _checkBoxSize.Width
            && p.Y >= _checkBoxLocation.Y && p.Y <=
                _checkBoxLocation.Y + _checkBoxSize.Height)
            {
                _checked = !_checked;
                if (OnCheckBoxClicked != null)
                {
                    OnCheckBoxClicked(_checked);
                    this.DataGridView.InvalidateCell(this);
                }

            }

            //base
            base.OnMouseClick(e);
        }
    }
}
